package com.touchealth.test.touchealth;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.util.Map;

@Slf4j
public class DemoAESUploadFileNoticePort {
    private String adress = "gismeitest.e-sleb.com";//gismeitest.sleb.cn 测试环境地址
    private int port = 8900;
    private String userPass = "mobile:mobile";
    private String openId = "123";
    public String getToken() throws IOException
    {
        CloseableHttpClient httpclient =  HttpClients.createDefault();
        try {
            String encoding = DatatypeConverter.printBase64Binary(userPass.getBytes("UTF-8"));
            HttpPost httpPost = new HttpPost("http://"+adress+":"+port+"/api-uaa/oauth/openId/token?openId="+openId);
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
            httpPost.setHeader("Authorization", "Basic " + encoding);
            System.out.println(encoding);
            HttpHost target = new HttpHost(adress, port, "http");
            CloseableHttpResponse response = httpclient.execute(target,httpPost);
            try {
                log.info("------------------task import----------------------");
                StatusLine tLine = response.getStatusLine();
                log.info(""+tLine);
                HttpEntity entity = response.getEntity();
                String data = IoUtil.read(entity.getContent(), "UTF-8");
                log.info(data);
                return data;
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

    private void test(String str) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        Map<String,Object> map = JSONUtil.toBean(str,Map.class);

        String toKen = (String) MapUtil.getAny(map,"datas").get("access_token");
        log.info("token: "+toKen);

        try {
            HttpPost httpPost = new HttpPost("http://"+adress+":"+port+"/api-lisOnline/uploadFileNoticePort");
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setHeader("Authorization", "Bearer "+toKen);
            String body = "{\n" +
                    "\t\"uploadSys\": \"EHP\",\n" +
                    "\t\"fileName\": \"test.json\",\n" +
                    "\t\"filePath\": \"asdfed/test.json\",\n" +
                    "\t\"fileType\": \":001/002/\"\n" +
                    "}";
            //加密报文
            String xBody = AESUtil.AESEncode(AESUtil.AESROULES, body);
            xBody = xBody.replaceAll("\\s*|\t|\r|\n","");
            log.info("加密报文： "+xBody);
            //生成签名
            String sign = SHAEncryptUtil.encrypt(xBody, SHAEncryptType.SHA256);
            log.info("sign: "+sign);
            httpPost.setHeader("sign", sign);
            StringEntity stringEntity = new StringEntity(xBody, "UTF-8");
            stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf8"));
            httpPost.setEntity(stringEntity);
            HttpHost target = new HttpHost(adress, port, "http");
            CloseableHttpResponse response = httpclient.execute(target, httpPost);
            try {
                log.info("------------------task import----------------------");
                StatusLine tLine = response.getStatusLine();
                log.info(""+tLine);
                HttpEntity entity = response.getEntity();
                String data = IoUtil.read(entity.getContent(), "UTF-8");
                log.info(data);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

    public static void main(String[] args) {

        DemoAESUploadFileNoticePort d = new DemoAESUploadFileNoticePort();
        try {
            d.test(d.getToken());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
